package com.xuxueli.applyModules.utils.testJsonPathFile;
import java.io.IOException;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class JsoupUtils {

    public static void main(String[] args) {

        JsoupUtils t = new JsoupUtils();
        //Document doc = t.getDocument("https://baijiahao.baidu.com/s?id=1667756469242968479&wfr=spider&for=pc");
        Document doc = t.getDocument("https://blog.csdn.net/u012050154/article/details/78269869");
        //文章标题
        Elements elements1 = doc.select(".article-title");//通过 class名称进行查找,获取标题标签
        System.out.println("文章标题： "+elements1.text());//从标签中获取标题

        //文章内容
        Elements elements2 = doc.select(".article-content");
        Elements elements3 = elements2.select(".bjh-p");
        System.out.println("文章内容： "+elements3.html());

//        //图片
//        Elements elements4 = doc.select(".large");
//        List<String> eachAttr = elements4.eachAttr("src");
//        for(int i=0; i<eachAttr.size();i++) {
//            System.out.println("图片连接"+i+":"+eachAttr.get(i));
//        }
        //文章来源
        Elements elements5 = doc.select(".author-name");
        System.out.println("文章来源: "+elements5.html());

        //发布时间
        Elements elements6 = doc.select(".date");
        Elements elements7 = doc.select(".time");
        System.out.println(elements6.html()+" "+elements7.html());
    }

    /**
     * @param url 访问路径
     * @return
     */
    public  Document getDocument (String url){
        try {
            //通过Jsoup获取资源，5000是设置连接超时时间，单位ms
            return Jsoup.connect(url).timeout(5000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}